package com.clear.honghefinance.biz.modules.system.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.clear.honghefinance.biz.modules.system.member.domain.entity.Member;
import com.clear.honghefinance.biz.modules.system.member.domain.vo.AdminDTO;
import com.clear.honghefinance.biz.modules.system.member.domain.dto.UpdateEmailAndNameDTO;
import com.clear.honghefinance.biz.modules.system.member.mapper.MemberMapper;
import com.clear.honghefinance.biz.modules.system.member.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clear.honghefinance.common.constant.CommonConstant;
import com.clear.honghefinance.common.exception.BizException;
import com.clear.honghefinance.common.service.TokenService;
import com.clear.honghefinance.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 程序员clear
 * @since 2024-08-05
 */
@Service
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Resource
    private TokenService<AdminDTO> tokenService;

    @Override
    public long register(long tenantId) {
        Member member = new Member();
        member.setTenantId(tenantId);
        member.setSysRoleIds("[" + CommonConstant.ROLE_MEMBER + "]");
        member.initDefault();
        this.save(member);
        return member.getId();
    }

    /**
     * 获取会员信息
     *
     * @param id 会员id
     * @return 会员信息
     */
    @Override
    public Member get(long id) {
        return this.getOne(Wrappers.<Member>lambdaQuery().eq(Member::getId, id));
    }

    /**
     * `修改邮箱和姓名
     *
     * @param dto
     * @return
     */
    @Override
    public boolean updateEmailAndName(UpdateEmailAndNameDTO dto) {
        if (log.isInfoEnabled()) {
            log.info("修改邮箱和姓名中，入参：{}", JSON.toJSONString(dto));
        }
        try {
            LambdaUpdateWrapper<Member> lambdaUpdateWrapper = Wrappers.<Member>lambdaUpdate()
                    // 目前只能删除自己的账套
                    .eq(Member::getId, tokenService.getThreadLocalTenantId())
                    .eq(Member::getDisable, false)
                    .eq(Member::getName, dto.getName())
                    .eq(Member::getEmail, dto.getEmail())
                    .set(Member::getUpdateTime, DateUtil.getSystemTime());
            if (this.update(lambdaUpdateWrapper)) {
                return true;
            } else {
                if (log.isInfoEnabled()) {
                    log.info("修改邮箱和姓名异常，入参：{}", JSON.toJSONString(dto));
                }
                throw new BizException("修改邮箱和姓名中异常");
            }
        } catch (Exception e) {
            log.error("修改邮箱和姓名中异常 {}", e.getMessage(), e);
            throw new BizException("修改邮箱和姓名中异常");
        }
    }
}
